package tool.file.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ReadFileUtil {

    public static void readAllFiles(String folderPath, List<String> allFiles) {

        File file = new File(folderPath);
        if (file.isDirectory()) {
            File files[] = file.listFiles();
            for (File f : files) {
                readAllFiles(f.getPath(), allFiles);
            }
        } else {
            allFiles.add(file.getPath());
        }
    }

    public static List<String> readAllFiles(String folderPath) {

        List<String> allFiles = new ArrayList<String>();
        readAllFiles(folderPath, allFiles);

        return allFiles;
    }

    public static List<String> readAllFilesExcept(String folderPath,
            String exceptNm) {

        List<String> allFiles = new ArrayList<String>();
        readAllFiles(folderPath, allFiles);

        List<String> finalList = new ArrayList<String>();
        for (String filePath : allFiles) {
            String newFilePath = filePath.toLowerCase();
            exceptNm = exceptNm.toLowerCase();
            if (newFilePath.indexOf(exceptNm) == -1) {
                finalList.add(filePath);
            }
        }

        return finalList;
    }

    public static List<String> readAllFilesByExNm(String folderPath, String exNm) {

        List<String> allFiles = new ArrayList<String>();
        readAllFiles(folderPath, allFiles);

        List<String> finalList = new ArrayList<String>();
        for (String filePath : allFiles) {
            String newFilePath = filePath.toLowerCase();
            exNm = exNm.toLowerCase();
            if (newFilePath.endsWith(exNm)) {
                finalList.add(filePath);
            }
        }

        return finalList;
    }
    
    public static List<String> changeFileEncode(List<String> files,
            String oldEncode, String newEncode) {

        List<String> backFileList = new ArrayList<String>();
        for (String file : files) {
            String backFile = new File(file).getPath() + "-backup";
            if(fileRename(file, backFile)) {
                changeFileEncode(backFile, file, oldEncode, newEncode);
                backFileList.add(file);
            }
        }

        return backFileList;
    }
    
    public static Boolean fileRename(String oldFilePath, String newFilePath) {
        
        File newFile = new File(newFilePath);
        Boolean result = new File(oldFilePath).renameTo(newFile);
        if(!result) {
            StringBuilder builder = new StringBuilder();
            builder.append("================Rename ");
            builder.append(oldFilePath);
            builder.append("================Failed!");
            System.out.println(builder);
        }
        
        return result;
    }
    
    public static void changeFileEncode(String oldFile, String newFile,
            String oldEncode, String newEncode) {

        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    oldFile), oldEncode));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(newFile), newEncode));

            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line + "\r\n");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static double getFileSize(File f) {

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            return fis.available();
        } catch (IOException e) {
            System.out.println("IO Error!");
        }

        return -1;
    }

    public static double getFileSize(String file) {

        File f = new File(file);
        return getFileSize(f);
    }

    public static double byteToKb(double size) {

        BigDecimal d = new BigDecimal(size / 1026.89655).setScale(2,
                BigDecimal.ROUND_HALF_UP);

        return d.doubleValue();
    }

    public static String readMedia() {

        return null;
    }
}
